﻿using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace BeatingMole
{
    class CBusButton
    {
        #region "Variable"
        CInfoButtonSettings InfoButtonSettings;
        ContentManager _Content;
        bool IsSoundOn = true;
        bool IsClick = true;
        float CurMediaVolumn = MediaPlayer.Volume;
        #endregion

        #region "Properties"

        #endregion

        #region "Constructor"
        public CBusButton(ContentManager content)
        {
            _Content = content;
            InfoButtonSettings = new CInfoButtonSettings(_Content);
        }
        #endregion

        #region "Method"
        public void IsClickSound(Dictionary<String, Rectangle> DIRectangle)
        {
            CGlobalVariable.mouseStateCurrent = Mouse.GetState();
            if (CGlobalVariable.mouseStateCurrent.LeftButton == ButtonState.Pressed && CGlobalVariable.mouseStatePrevious.LeftButton == ButtonState.Released)
            {
                Rectangle BtnSoundOn = new Rectangle(730, 0, DIRectangle["button_sound_on"].Width, DIRectangle["button_sound_on"].Height);
                if (BtnSoundOn.Contains(new Point((int)Mouse.GetState().X, (int)Mouse.GetState().Y)))
                {
                    if (IsClick == false)
                    {
                        IsSoundOn = true;
                        IsClick = true;                        
                        MediaPlayer.Volume = CurMediaVolumn;
                        SoundEffect.MasterVolume = CurMediaVolumn;
                        CGlobalVariable.IsMute = true;
                    }

                    else if (IsClick == true)
                    {
                        IsSoundOn = false;
                        IsClick = false;
                        CurMediaVolumn = MediaPlayer.Volume;
                        MediaPlayer.Volume = 0;
                        SoundEffect.MasterVolume = 0;
                        CGlobalVariable.IsMute = false;
                    }
                }
            }
            CGlobalVariable.mouseStatePrevious = CGlobalVariable.mouseStateCurrent;
        }

        public void UpdateButton(GameTime gameTime)
        {
            IsClickSound(InfoButtonSettings.XmlSpriteBatch);
        }
        public void DrawButton(SpriteBatch spriteBatch)
        {
            if (IsSoundOn == true)
            {
                spriteBatch.Draw(InfoButtonSettings.SpriteBatch, new Vector2(730, 0), InfoButtonSettings.XmlSpriteBatch["button_sound_on"], Color.White);
            }
            else
            {
                spriteBatch.Draw(InfoButtonSettings.SpriteBatch, new Vector2(730, 0), InfoButtonSettings.XmlSpriteBatch["button_sound_off"], Color.White);
            }
        }
        #endregion
    }
}
